package com.ivr.cm.http;

import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import com.ivr.cm.data.BaseData;
import com.ivr.cm.handler.BaseHandler;
import com.ivr.cm.handler.HandlerFactory;

/**
 * Http模拟VSPP网关调用
 * 模拟挂机：http://127.0.0.1:8080/IvrGateway_my/vspp.do?serviceid=07771&operateid=0601&calling=15210837706&called=1259077711&seqNo=123&body=15210837706|1259077711|2016-02-21%2023:20:02|2016-02-21%2023:22:20
 * 模拟鉴权：http://127.0.0.1:8080/IvrGateway_my/vspp.do?serviceid=07771&operateid=00001&calling=15210837706&called=1259077711&seqNo=123&body=15210837706|1259077711|2016-02-21%2023:20:00|00|||||
 * 只需传入对应的serviceid,operateid,calling,called以及模拟数据body即可
 * 
 * @author liugeng
 * @date 2015-12-18
 *
 */
public class HttpController implements Controller {
	private static Logger logger = Logger.getLogger(HttpController.class);
	
	public HandlerFactory handlerFactory;

	@Override
	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		
		String serviceid = request.getParameter("serviceid");
		String operateid = request.getParameter("operateid");
		String calling = request.getParameter("calling");
		String called = request.getParameter("called");
		String seqNo = request.getParameter("seqNo"); 
		String body = request.getParameter("body");
		
		logger.info(String.format("[%s][%s]HTTP传入信息[serviceid=%s operateid=%s body=%s]", called,calling,serviceid,operateid,body));
		
		//获取业务处理handler
		BaseHandler handler;
		if(called!=null && called.length()>=9){
			handler = handlerFactory.getHandler(called.substring(0,9),serviceid,operateid);
		}else{
			return null;
		}
		
		//业务处理
		BaseData data = new BaseData();
		data.setServiceId(serviceid);
		data.setOperateId(operateid);
		data.setLong_called(called);
		data.setShort_called(called.substring(0,9));
		data.setCalling(calling);
		data.setBody(body);
		
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");
		String nowDateStr = format.format(new Date());
		
		if(seqNo != null && !"".equals(seqNo)){
			int seqNoLength = seqNo.length();
			if(seqNoLength > 6){
				seqNo = seqNo.substring(seqNoLength - 6, seqNoLength);
			}else if(seqNoLength < 6){
				String prv_seqNo = "";
				for(int i = 0; i < (6 - seqNoLength); i++){
					prv_seqNo += "0";
				}
				
				seqNo = prv_seqNo + seqNo;
			}
			
			data.setLinkId(nowDateStr + seqNo);
		}
		
		logger.info(String.format("[%s][%s]开始业务处理,处理类:%s", data.getLong_called(), data.getCalling(),handler.getClass().getSimpleName()));
		
		long start_time = System.currentTimeMillis();
		String resp = handler.doAction(data);
		long end_time = System.currentTimeMillis();
		logger.info(String.format("[%s][%s]业务处理完成,耗时%dms",data.getLong_called(),data.getCalling(),(end_time-start_time)));
		
		OutputStream os = response.getOutputStream();
		os.write(resp.getBytes());
		os.flush();
		os.close();
		
		return null;
	}

	public HandlerFactory getHandlerFactory() {
		return handlerFactory;
	}

	public void setHandlerFactory(HandlerFactory handlerFactory) {
		this.handlerFactory = handlerFactory;
	}
}